package yl.hs.bmipfull.service.equipment.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.equipment.IEquipmentDAO;
import yl.hs.bmipfull.daos.equipment.IInspectionDao;
import yl.hs.bmipfull.daos.flow.IFlowUserTaskDao;
import yl.hs.bmipfull.pojo.equipment.EquipmentEntity;
import yl.hs.bmipfull.pojo.equipment.DeviceInspectionEntity;
import yl.hs.bmipfull.pojo.flow.FlowUserTaskEntity;
import yl.hs.bmipfull.service.equipment.IInspectionService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.MsgType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.SubmitModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
 * InspectionServiceImpl
 * @category
 * @author 谢景广
 * @date 2022-11-15
 */
@Service
public class InspectionServiceImpl extends EquipmentBaseService implements IInspectionService {

    @Override
    public PageResult<Map<String,Object>> queryPage(PageQuery query, int rowNum, int page) {
        PageResult<Map<String,Object>> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(query.getFilters(),
                DeviceInspectionEntity.class);
        result.setPage(page);
        query.getQuery().put("querySkip", (page - 1) * rowNum);
        query.getQuery().put("pageSize", rowNum);
        query.checkSortSides(DeviceInspectionEntity.class);
        query.getQuery().put("sortSide", String.join(",",query.getSortSides()));
        result.setRecords(insDao.queryCountByCondition(query.getQuery(),filters));
        var dataList = insDao.queryListByCondition(query.getQuery(),filters);
        List<Map<String,Object>> bList = new ArrayList<>();
        if(dataList.size()>0){
            for( var data:dataList){
                var map = data.toMap();
                if(data.getProcessState()==0){
                    map.put("processState","");
                    map.put("reorganizeState","");
                }
                bList.add(map);
            }
        }
        result.setData(bList);
        return result;
    }



    @Override
    public Integer save(SubmitModel model) throws Exception {
        var result = 0;
        DeviceInspectionEntity entity;
        if(Tl.isEmpty(model.getSn())){
            entity = new DeviceInspectionEntity();
            Tl.getChangeLog(entity,model.getData());
            entity.setSn(queryNewSN(entity.getClass()));
            result = insDao.insertNew(entity);
            if("异常".equals(entity.getInspectionState()) && entity.getProcessState()==2){
                var equipment = equipmentDAO.findBySN(entity.getEquipmentSN());
                sendMsg(MsgType.Instant, entity.getProcessorSN(), equipment.getName()+"【"+equipment.getDeviceNum()+"】有异常情况",
                        entity.getInspectionUserName()+"发现设备:"+equipment.getName()+"【"+equipment.getDeviceNum()+"】"+entity.getInspectionDesc());
                addTask(entity.getSn(),"设备巡检",entity.getInspectionUserSN(),entity.getInspectionUserName(),entity.getProcessorSN());
            }
            writeLog(ActionType.CREATE,entity.getSn(),"",null,entity.getClass());
            writeLog(ActionType.CREATE,entity.getEquipmentSN(),"%s创建新的编号为"+entity.getSn()+"的巡检信息",EquipmentEntity.class);
        }
        else{
            entity = insDao.findBySN(model.getSn());
            var backJson = entity.toJson();
            var equipment = equipmentDAO.findBySN(entity.getEquipmentSN());
            if("异常".equals(entity.getInspectionState()) && entity.getProcessState()==2 ){
                if(entity.getReorganizeState()==0){
                    sendMsg(MsgType.Instant, entity.getProcessorSN(), equipment.getName()+"【"+equipment.getDeviceNum()+"】有异常情况",
                            entity.getInspectionUserName()+"发现设备:"+equipment.getName()+"【"+equipment.getDeviceNum()+"】"+
                                    entity.getInspectionDesc()+",需要做整改："+entity.getReorganize());
                    addTask(entity.getSn(),"设备巡检问题",entity.getInspectionUserSN(),entity.getInspectionUserName(),entity.getProcessorSN());
                }
                if(entity.getReorganizeState()==1){
                    sendMsg(MsgType.Instant, entity.getInspectionUserSN(), equipment.getName()+"【"+equipment.getDeviceNum()+"】整改已完成",
                            entity.getProcessorName()+"对设备:"+equipment.getName()+"【"+equipment.getDeviceNum()+"】"+entity.getReorganize()+
                                    "，已完成整改。整改结果："+entity.getReorganizeResult());
                    addTask(entity.getSn(),"设备巡检整改完成",entity.getProcessorSN(),entity.getProcessorName(),entity.getInspectionUserSN());
                }
                if(entity.getReorganizeState()==3){
                    sendMsg(MsgType.Instant, entity.getProcessorSN(), equipment.getName()+"【"+equipment.getDeviceNum()+"】整改需要返工",
                            entity.getInspectionUserName()+"验收设备:"+equipment.getName()+"【"+equipment.getDeviceNum()+"】整改不通过，"+entity.getReorganize()
                                    );
                    addTask(entity.getSn(),"设备巡检整改返工",entity.getInspectionUserSN(),entity.getInspectionUserName(),entity.getProcessorSN());
                }
            }

            var changeLog = Tl.getChangeLog(entity,model.getData());
            writeLog(ActionType.UPDATE,entity.getSn(),changeLog,backJson,entity.getClass());
            writeLog(ActionType.UPDATE,entity.getEquipmentSN(),"%s更新了编号为"+entity.getSn()+"的巡检信息:"+changeLog,EquipmentEntity.class);
            result = insDao.update(entity);
        }
        if(result>0){
            var user = HsApp.getCurrent();
            saveGS(
                    entity.getSn(),
                    entity.getEquipmentName(),
                    String.join(",",
                            entity.getEquipmentName(),
                            entity.getInspectionDesc(),
                            entity.getInspectionUserName(),
                            entity.getProcessorName(),
                            entity.getReorganize(),
                            entity.getReorganizeResult(),
                            entity.getRemark()
                    ),
                    entity,
                    HsApp.getCurrent() != null ? HsApp.getCurrent().getUserSN() : null,
                    user.getOrganizationCode()
            );
        }
        return result;
    }



    @Override
    public Integer delete(String[] snArray ){
        var result = insDao.changeState(snArray,-1);
        if(result> 0){
            for (var sn : snArray) {
                writeLog(ActionType.DELETE, sn,"","", EquipmentEntity.class);
            }
            deleteGs(snArray);
        }
        return  result;
    }

    @Override
    public List<DeviceInspectionEntity> queryPendingInspection() {
        var user = HsApp.getCurrent();
        return insDao.queryPendingByUserSN(user.getUserSN());
    }

    private IInspectionDao insDao;

    @Autowired
    public void setInsDao(IInspectionDao insDao) {
        this.insDao = insDao;
    }

    private IEquipmentDAO equipmentDAO;

    @Autowired
    public void setEquipmentDAO(IEquipmentDAO equipmentDAO) {
        this.equipmentDAO = equipmentDAO;
    }



}
